package com.cit.util;

import java.io.File;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;

import org.w3c.dom.Document;
/**
 * 
 * @author Lang Yu(lang.yu@hp.com)
 * @time Mar 7, 2011 12:12:59 PM
 * @file FileUtil.java
 * @package com.cit.util
 * @version 1.0
 * @see
 * This is a tool class to process some file actions.
 */
public class FileUtil {
	/**
	 * Initialize the validate file name with some rules.
	 * @param rootName
	 * @param contextName
	 * @param servletType
	 * @return
	 */
	public static String getObjectName(String rootName,String contextName,String servletType){
		StringBuilder builder = new StringBuilder();
		builder.append("Rules-").append(contextName).append("_").append(servletType).append("_").append(rootName);
		return builder.toString();
	}
	/**
	 * 
	 * @param rootName
	 * @param contextName
	 * @param servletType
	 * @return
	 */
	public static String getRespObjectName(String rootName,String contextName,String servletType){
		StringBuilder builder = new StringBuilder();
		builder.append("RulesResponse-").append(contextName).append("_").append(servletType).append("_").append(rootName);
		return builder.toString();
	}
	/**
	 * Get the file name of output
	 * @param rootName
	 * @param contextName
	 * @param servletType
	 * @return
	 */
	public static String getOutFileName(String rootName,String contextName,String servletType,Date timeStamp){
		StringBuilder builder = new StringBuilder();
		builder.append(contextName).append("_").append(servletType).append("_").append(rootName).append("_");
		DateFormat format = new SimpleDateFormat("yyyyMMddHHmmssSSS");
		builder.append(format.format(timeStamp)).append(".xml");
		return builder.toString();
	}
	/**
	 * Convert the field configuration xml file to a Document object in java language,
	 * we'll validate the field required rull with the Document object.
	 * @param configFilePath
	 * @return
	 * @throws Exception
	 */
	public static Document readValidateConfig(String configFilePath)
			throws Exception {
		Document doc = null;
		DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
		DocumentBuilder builder = null;
		try {
			builder = factory.newDocumentBuilder();
			doc = builder.parse(new File(configFilePath));
		} catch (Exception ex) {
			//ex.printStackTrace();
			doc = null;
		}
		return doc;
	}
	/**
	 * Comments: the output folder must be CanRead by Connect-IT and CanWrite by our web application
	 * ================================================
	 * Check the configuration folder's attributes, just as IsHidden,CanRead,CanWrite
	 * @param rootPath
	 * @return
	 */
	public static boolean checkFolders(String rootPath){
		File file = new File(rootPath);
		if( file.exists()){
			File[] subFolders = file.listFiles();
			if( subFolders.length == 4){
				return true;
			}else{
				return false;
			}
		}else{
			return false;
		}
	}
	/**
	 * Initialize the output folder all in web.xml
	 * @return
	 */
	public static boolean createFolders(String[] folders){
		boolean result = false;
		try{
			for (String subFolder : folders) 
			{
				File file = new File(subFolder);
				file.mkdirs();
			}
			result = true;
		}catch(Exception ex){
			ex.printStackTrace();
			result = false;
		}
		return result;
	}
	/**
	 * Initialize the output folder, if not exist, we'll create a new one.
	 * @param rootPath
	 * @return
	 */
	public static boolean createFolders(String rootPath) {
		boolean result = true;
		if( checkFolders(rootPath)){
			return true;
		}
		try {
			File file = new File(rootPath);
			if (!file.exists()) {
				file.mkdirs();
				createSubFolders(file.getAbsolutePath());
				result = true;
			} else {
				if (file.isDirectory() && file.canWrite()
						&& file.list().length == 0) {
					createSubFolders(file.getAbsolutePath());
					result = true;
				} else {
					result = false;
				}
			}
		} catch (Exception ex) {
			ex.printStackTrace();
			result = false;
		}
		return result;
	}
	/**
	 * Filter the folder's name
	 * @param parentPath
	 * @throws Exception
	 */
	private static void createSubFolders(String parentPath) throws Exception {
		String[] outputFolders = new String[] { "Xml", "Cfg", "Resp", "Org" };
		for (String subFolder : outputFolders) {
			File file = new File(parentPath + "/" + subFolder);
			file.mkdir();
		}
	}
}
